package com.commons.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.*;
import com.commons.service.entity.*;
import com.commons.service.service.*;
import com.commons.tools.utils.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  对多对，3张表（1张关系表）
 * </p>
 *
 * @author robot
 * @since 2023-12-05
 */
@RestController
@RequestMapping("/service/course")
public class CourseController {
    @Autowired
    private IClassesService classesService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IRUserCourseService userCourseService;
    @Autowired
    private ICourseService courseService;

    @ApiOperation("根据用户查询课程列表")
    @GetMapping(value = "/listByUserId")
    public Result<List<Course>> listByUserId(@RequestParam Long userId) {
        LambdaQueryWrapper<RUserCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RUserCourse::getUserId, userId);
        List<RUserCourse> rlist = userCourseService.list(wrapper);

        List<Course> list = new ArrayList<>();
        rlist.forEach(item -> {
            int courseId = item.getCourseId();
            Course course = courseService.getById(courseId);
            list.add(course);
        });

        return Result.ok(list);
    }

}

